import {
  createAction,
  handleActions
} from 'redux-actions'
import {
  CALL_API
} from 'redux-api-middleware'

import getApiPath from '../../helpers/getApiPath'
import getFetchOptions from '../../helpers/getFetchOptions'
import getQuery from '../../helpers/getQuery'

// ------------------------------------
// Constants (Action Types)
// ------------------------------------

// pending request
const FETCH_REQUEST = Symbol('FETCH_REQUEST')
const FETCH_FAILURE = Symbol('FETCH_FAILURE')
const FETCH_DETAIL_SUCCESS = Symbol('FETCH_DETAIL_SUCCESS')
const FETCH_LIST_SUCCESS = Symbol('FETCH_LIST_SUCCESS')
const ZAN_DIS_SUCCESS = Symbol('ZAN_DIS_SUCCESS')
// ------------------------------------
// Actions (Action Creator)
// ------------------------------------


export const addView = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/AddViewCount', params), 'POST', {
  })

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('ADD_VIEW_SUCCESS'),
          payload: (action, state, json) => json.data
        },
        FETCH_FAILURE
      ]
    }
  }
}


export const fetchDetail = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/GetArticleDetails', params))

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: FETCH_DETAIL_SUCCESS,
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}


export const fetchDisList = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/GetCommentByArticleId', params))

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: FETCH_LIST_SUCCESS,
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const deleteDis = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/DelComment', params), 'post')

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('DELETE_DIS_SUCCESS'),
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}


export const addDis = (params, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/AddComment'),'post',{
    body: JSON.stringify(params)
  })

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: Symbol('ADD_DIS_SUCCESS'),
          payload: (action, state, json) => json.Data
        },
        FETCH_FAILURE
      ]
    }
  }
}

export const zanDis = (id, index, callback) => {

  const fetchOptions = getFetchOptions(getApiPath('Article/ThumbUp', {id, isUp: true}), 'post')

  return {
    [CALL_API]: {
      ...fetchOptions,
      complete: callback,
      types: [
        FETCH_REQUEST,
        {
          type: ZAN_DIS_SUCCESS,
          payload: (action, state, json) => Object.assign({},json.Data, {index})
        },
        FETCH_FAILURE
      ]
    }
  }
}


/**
 * 暴露actions到外面，方便使用react-redux connect绑定到Container Component
 */
export const actions = {
  addView, 
  fetchDetail,
  fetchDisList,

  deleteDis,
  addDis,
  zanDis
}

// ------------------------------------
// Reducers
// ------------------------------------
const initialState = {
  pending: false,

  list: [],
  pageIndex: 0,
  pageCount: 0
}

export default handleActions({

  [FETCH_REQUEST](state) {
    return {
      ...state,
      pending: true
    }
  },

  [FETCH_FAILURE](state) {
    return {
      ...state,
      pending: false
    }
  },

  [FETCH_LIST_SUCCESS](state, {payload}) {
    console.log(payload)
    let list = payload.PageIndex == 1 ? payload : state.list.concat(payload)
    return {
      ...state,
      pending: false,
      list: list,
      pageCount: +payload.PageCount,
      pageIndex: +payload.PageIndex
    }
  },

  [ZAN_DIS_SUCCESS](state, {payload}) {
    state.list[payload.index].GoodCount += 1
    return  {
      ...state,
      list: state.list.slice() 
    }
  }

}, initialState)
